/**
 * Copyright (c) 2015-present, MaxLeapMobile.
 * All rights reserved.
 * ----
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 */
package com.yun.baymax.activity.account;

import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;

import com.yun.baymax.R;
import com.yun.baymax.activity.BaseActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;

/**
 * @author Walking
 */
public class ForgetPasswordActivity extends BaseActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.progress_bar_area)
    View progressbar;
    @BindView(R.id.forget_password_tel)
    TextInputLayout forgetPwdTel;
    @BindView(R.id.forget_password_confirm)
    Button confirmBtn;

    private String email;
    private BmobUser currentUser;

    @Override
    protected int setLayoutResourceId() {
        return R.layout.maccount_activity_forget_password;
    }

    @Override
    protected void initVariables() {
        ButterKnife.bind(this);
        email = getIntent().getStringExtra("title");
        currentUser = BmobUser.getCurrentUser();
        if (currentUser != null) {
            forgetPwdTel.getEditText().setText(email);
        }
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        forgetPwdTel.getEditText().addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                email = s.toString();
                if (!TextUtils.isEmpty(email) && email.contains("@") && !email.startsWith("@") && !email.endsWith("@")) {
                    confirmBtn.setSelected(true);
                    confirmBtn.setClickable(true);
                } else {
                    confirmBtn.setSelected(false);
                    confirmBtn.setClickable(false);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        confirmBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String email = forgetPwdTel.getEditText().getText().toString().trim();

                if (TextUtils.isEmpty(email) || !email.contains("@") || email.startsWith("@") || email.endsWith("@")) {
                    showToast("请输入正确的邮箱");
                    return;
                }
                BmobUser.resetPasswordByEmail(email, new UpdateListener() {
                    @Override
                    public void done(BmobException e) {
                        if (e == null) {
                            showToast("重置密码请求成功，请到邮箱进行密码重置操作");
                            finish();
                        } else {
                            if (e.getMessage().contains("no user found"))
                                showToast("未绑定该邮箱");
                            else
                                showToast("失败:" + e.getMessage());
                        }
                    }
                });

                /*txt = forgetPwdTel.getEditText().getText().toString().trim();
                if (TextUtils.isEmpty(txt)) {
                    forgetPwdTel.setError(getString(R.string.fragment_login_tel_empty_error));
                    forgetPwdTel.requestFocus();
                } else if (!NoUtilCheck.isMobileNo(txt)) {
                    forgetPwdTel.setError(getString(R.string.fragment_login_tel_invalid_error));
                    forgetPwdTel.requestFocus();
                } else {
                    forgetPwdTel.setErrorEnabled(false);
                    forgetPwdTel.setError("");
                    View view = getCurrentFocus();
                    ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)).
                            hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                    progressbar.setVisibility(View.VISIBLE);

                    *//*MLUserManager.requestPasswordResetByPhoneNumberInBackground(txt, new RequestPasswordResetCallback() {
                        @Override
                        public void done(final MLException e) {
                            progressbar.setVisibility(View.GONE);
                            if (e != null) {
                                //  发生错误
                                showToast(e.getMessage());
                            } else {
                                //  成功请求
                                showToast("发送成功");

                                Intent i = new Intent(ForgetPasswordActivity.this, ResetPasswordActivity.class);

                                i.putExtra(ResetPasswordActivity.INTENT_KEY_RESET_PHONE, txt);
                                startActivityForResult(i, 1);
                                finish();
                            }
                        }
                    });*//*

                }*/
            }
        });

        /*forgetPwdTel.getEditText().addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() < 6) {
                    confirmBtn.setEnabled(false);
                } else {
                    confirmBtn.setEnabled(true);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        if (forgetPwdTel.getEditText().getText().toString().length() < 6) {
            confirmBtn.setEnabled(false);
        } else {
            confirmBtn.setEnabled(true);
        }*/
    }

    @Override
    protected void loadData() {

    }

}
